
var formData = {
    gsmc : "",
    thcq : "",
    thrqc : "",
    thrqz : "",
    thry : "",
    cphm : "",
    sjhm : "",
    thcp : "",
    thdw : "",
    thsl : ""   
}

var opts = {
    gsmc : [],
    shcq : [],
    cpmc : []
}


$(document).ready(()=>{

    init();

    loadCompanyList();
    loadCheckFactoryList();
    loadProductList();


    addEvent();

})

/**加载公司列表 */
function loadCompanyList(){
    $.ajax({
        url : REQUEST_URL,
        type : "post",
        headers : REQUEST_HEADERS(),
        contentType:"application/json",
        data: JSON.stringify({action : "UserAuthCompany"}),
        success:function(r,t,x){
            opts['gsmc']=r.code == 200 ? r.list[0].Rows : []
        }
    })
}

/**加载审核厂区 */
function loadCheckFactoryList(){
    $.ajax({
        url : REQUEST_URL,
        type : "post",
        contentType:"application/json",
        headers : REQUEST_HEADERS(),
        data: JSON.stringify({action : "UserAuthFactory"}),
        success:function(r,t,x){
            opts['shcq']=r.code == 200 ? r.list[0].Rows : []
        }
    })
}

/**加载提货产品 */
function loadProductList(){
    $.ajax({
        url : REQUEST_URL,
        type : "post",
        contentType:"application/json",
        headers : REQUEST_HEADERS(),
        data: JSON.stringify({action : "UserAuthProduct"}),
        success:function(r,t,x){
            opts['cpmc']=r.code == 200 ? r.list[0].Rows : []
        }
    })
}

/**设置初始值-日期组件 */
function initialValue(){
    const now = new Date();
    formData['thrqc'] = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`
    formData['thrqz'] = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`
}

/**
 * 监听可选事件
 */
function addEvent(){
    $("#cphm").click(onClickCPHM)
    $("#gsmc").click(onClickGSMC)
    $("#thcq").click(onClickTHCQ)
    $("#thcp").click(onClickTHCP)
    $("#back-btn").click(onClickBack)
    $("#submit-btn").click(onClickSubmit);
}

function onClickBack(){
    history.go(-1)
}

function onClickSubmit(){
    /**
     * CustomNo : data.gsmc,
            TakeCompany : data.thdw,
            CustomName : opt.companyList.filter(item=>item.value == data.gsmc)[0].label,
            CompanyNumber : data.thcq,
            CompanyName : opt.factoryList.filter(item=>item.value == data.thcq)[0].label,
            TakeDateFrom : `${data.thrqc.getFullYear()}-${data.thrqc.getMonth()+1}-${data.thrqc.getDate()}`,
            TakeDateEnd : `${data.thrqz.getFullYear()}-${data.thrqz.getMonth()+1}-${data.thrqz.getDate()}`,
            TakeUserName : data.thry,
            SalesManNo:"",
            SalesManName:"",
            CarNum : data.cphm,
            phoneNumber : data.sjhm,
            ProductType : data.thcp,
            TakeQuantity:parseFloat(data.thsl),
     */
    const param = {
        action : "PickingOrderCreate",
        CustomNo : formData.gsmc,
        TakeCompany : formData.thdw,
        CustomName : formData.gsmc_str,
        CompanyNumber : formData.thcq,
        CompanyName : formData.thcq_str,
        TakeDateFrom : $("#thrqc").val(),
        TakeDateEnd : $("#thrqz").val(),
        TakeUserName : $("#thry").val(),
        SalesManName : "",
        SalesManNo : "",
        CarNum : formData.cphm,
        phoneNumber : $("#sjhm").val(),
        ProductType : formData.thcp,
        TakeQuantity : parseFloat($("#thsl").val())

    };
    console.log(param)
    if(param.CustomNo==""){
        layer.msg("公司名称不能为空")
        return ;
    }
    if(param.CompanyNumber==""){
        layer.msg("提货厂区不能为空")
        return ;
    }
    if(param.TabkeDateFrom==""){
        layer.msg("提货日期起始时间不能为空");
        return ;
    }
    if(param.TakeUserName==""){
        layer.msg("提货人员不能为空");
        return ;
    }
    if(param.phoneNumber=="" || param.phoneNumber.length!=11){
        layer.msg("提货人手机号不合法");
        return ;
    }
    if(param.ProductType==""){
        layer.msg("提货产品不能为空");
        return ;
    }
    if(param.CarNum.length != 7 && param.CarNum.length!=8){
        layer.msg("车牌号不正确");
        return ;
    }
    if(param.TakeQuantity<=0){
        layer.msg("提货数量不能为小于0");
        return ;
    }
  
    $.ajax({
        url : REQUEST_URL,
        data : JSON.stringify(param),
        type : "post",
        contentType:"application/json",
        headers : REQUEST_HEADERS(),
        success:function(r,t,x,){
            const res = responseHandler(r,t,x);
            layer.msg(res.message);
            if(res.code == 200){
                cleanCPH()
                changeEditMode();
            }
            
        }
    })
}

function changeEditMode(){
    $("#gsmc").unbind();
    $("#thcq").unbind();
    $("#thrqc").attr("disabled","disabled")
    $("#thrqz").attr("disabled","disabled")
    
    $("#thry").focus(()=>{
        $("#thry").blur();
    });
    $("#cphm").unbind()
    $("#thcp").unbind();
    $("#thsl").focus(()=>{
        $("#thsl").blur();
    })
    $("#sjhm").focus(()=>{
        $("#sjhm").blur();
    })
    $("#submit-btn").css({"display":"none"})
}

function onClickTHCQ(){
    $("#tmp").children().remove();
    opts.shcq.forEach(item=>{
        console.log(item)
        const ele = `
            <div id="gsh_${item.GSH}" class='opt-item'>${item.GSMC}</div>
        `
        $("#tmp").append(ele);
        $("#gsh_"+item.GSH).click(function(){
            $("#gsh_"+formData.thcq).css({"background-color":"white","color":"black"})
            $("#gsh_"+item.GSH).css({"background-color":"rgb(229,86,77)","color":"white"})
            formData.thcq = item.GSH;
            formData.thcq_str = item.GSMC;
        })
    })

    layer.open({
        title : "请选择提货厂区",
        content : $("#tmp"),
        area:["90%","400px"],
        type:1,
        closed:false,
        btn:["确定"],
        end:function(){
            $("#thcq").text(formData.thcq_str);
            $("#tmp").children().remove();
        }
    })
}

function onClickGSMC(){
    $("#tmp").children().remove();
    opts.gsmc.forEach(item=>{
        console.log(item)
        const ele = `
            <div id="gsh_${item.KSBH}" class='opt-item'>${item.KSMC}</div>
        `
        $("#tmp").append(ele);
        $("#gsh_"+item.KSBH).click(function(){
            $("#gsh_"+formData.gsmc).css({"background-color":"white","color":"black"})
            $("#gsh_"+item.KSBH).css({"background-color":"rgb(229,86,77)","color":"white"})
            formData.gsmc = item.KSBH;
            formData.gsmc_str = item.KSMC;
        })
    })

    layer.open({
        title : "请选择公司名称",
        content : $("#tmp"),
        area:["90%","400px"],
        type:1,
        closed:false,
        btn:["确定"],
        end:function(){
            $("#gsmc").text(formData.gsmc_str);
            $("#tmp").children().remove();
        }
    })
}
function onClickTHCP(){
    $("#tmp").children().remove();
    opts.cpmc.forEach(item=>{
        console.log(item)
        const ele = `
            <div id="thcp_${item.THCP}" class='opt-item'>${item.THCP}</div>
        `
        $("#tmp").append(ele);
        $("#thcp_"+item.THCP).click(function(){
            $("#thcp_"+formData.thcp).css({"background-color":"white","color":"black"})
            $("#thcp_"+item.THCP).css({"background-color":"rgb(229,86,77)","color":"white"})
            
            formData.thcp = item.THCP;
            formData.thcp_str = item.THCP;
            formData.thdw = item.THDW;
        })
    })

    layer.open({
        title : "请选择提货产品",
        content : $("#tmp"),
        area:["90%","400px"],
        type:1,
        closed:false,
        btn:["确定"],
        end:function(){
            $("#thcp_str").text(formData.thcp);
            $("#thdw").text(formData.thdw)
            $("#tmp").children().remove();
        }
    })
}



/**
 * 点击车牌号
 */
function onClickCPHM(){
    layer.open({
        type : 2,
        title:"编辑车牌号码",
        area:["100%","80%"],
        content : "/v1/vendor/cph/cph.html",
        end:function(){
            renderForEle("#cph",getCPH().join(""))
            formData['cphm']=getCPH().join("")
        }
    })
}

function renderForEle(id,val){
    $(id).text(val);
    
}